(function () {
    function CoolModule() {// 这个函数在这里就扮演了一个模块实例生成器的角色，每次调用都会生成一个独立的实例
        var something = "cool";
        var another = [1, 2, 3];

        function doSomething() {
            console.log(something);
        }

        function doAnother() {
            console.log(another.join(" ! "));
        }

        return {
            doSomething: doSomething,
            doAnother: doAnother
        };
    }
    var foo = CoolModule();//这里只返回了函数，将变量隐藏了。
    foo.doSomething(); // cool
    foo.doAnother(); // 1 ! 2 ! 3
})();


//单例
(function () {
    var foo = (function CoolModule() {
        var something = "cool";
        var another = [1, 2, 3];

        function doSomething() {
            console.log(something);
        }

        function doAnother() {
            console.log(another.join(" ! "));
        }

        return {
            doSomething: doSomething,
            doAnother: doAnother
        };
    })();
    foo.doSomething(); // cool
    foo.doAnother(); // 1 ! 2 ! 3
})();


// 模块构造器
var MyModules = (function Manager() {
    var modules = {};

    function define(name, deps, impl) {
        for (var i = 0; i < deps.length; i++) {
            deps[i] = modules[deps[i]];
        }
        modules[name] = impl.apply(impl, deps);
    }

    function get(name) {
        return modules[name];
    }

    return {
        define: define,
        get: get
    };
})();

MyModules.define("bar", [], function () {
    function hello(who) {
        return "Let me introduce: " + who;
    }

    return {
        hello: hello
    };
});

MyModules.define("foo", ["bar"], function (bar) {
    var hungry = "hippo";

    function awesome() {
        console.log(bar.hello(hungry).toUpperCase());
    }

    return {
        awesome: awesome
    };
});

var bar = MyModules.get("bar");
var foo = MyModules.get("foo");

console.log(
    bar.hello("hippo")
); // Let me introduce: hippo

foo.awesome(); // LET ME INTRODUCE: HIPPO